﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using IFDB.Models.Contexts;
using IFDB.Models.Tables;
using IFDB.ResourceLibrary;
using IFDB.ViewModels.PartialViewModels.Interfaces;

namespace IFDB.ViewModels.PartialViewModels
{
    public class TeamPersonListViewModel : ITeamPersonListViewModel
    {
        public TeamPersonListViewModel(Team team, DateTime startDate, DateTime endDate)
        {
            this.Team = team;
            this.StartDate = startDate;
            this.EndDate = endDate;
        }

        public Team Team { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }

        public List<AppearanceItemViewModel> personList;
        public List<AppearanceItemViewModel> PersonList
        {
            get
            {
                if (personList == null)
                {
                    List<AppearanceItemViewModel> appearanceList = new List<AppearanceItemViewModel>();

                    IEnumerable<AppearanceEvent> appearanceEvents;
                    using (IFDBContext db = new IFDBContext())
                        appearanceEvents = db.AppearanceEvents
                            .Include(a => a.Person)
                            .Where(a => a.TeamId == Team.TeamId && a.Match.MatchDate >= StartDate && a.Match.MatchDate <= EndDate)
                            .ToList();

                    foreach (AppearanceEvent appearanceEvent in appearanceEvents)
                    {
                        if (appearanceList.Count(a => a.Person.PersonId == appearanceEvent.PersonId) == 0)
                            appearanceList.Add(new AppearanceItemViewModel() { Person = appearanceEvent.Person });

                        AppearanceItemViewModel appearanceItem = appearanceList.Single(a => a.Person.PersonId == appearanceEvent.PersonId);

                        switch (appearanceEvent.MatchEventType)
                        {
                            case MatchEventType.Started:
                            case MatchEventType.Substitution:
                                appearanceItem.Played++;
                                break;

                            case MatchEventType.Goal:
                                appearanceItem.Goals++;
                                break;

                            case MatchEventType.OwnGoal:
                                appearanceItem.OwnGoals++;
                                break;

                            case MatchEventType.YellowCard:
                                appearanceItem.YellowCards++;
                                break;

                            case MatchEventType.RedCard:
                                appearanceItem.RedCards++;
                                break;
                        }
                    }

                    appearanceList.Sort();

                    personList = appearanceList;
                }

                return personList;
            }
        }

        public string GetPosition(Position position)
        {
            switch (position)
            {
                case Position.Goalkeeper:
                    return "Goalkeepers";

                case Position.Defender:
                    return "Defenders";

                case Position.Midfielder:
                    return "Midfielders";

                case Position.Forward:
                    return "Forwards";

                default:
                    return string.Empty;
            }
        }
    }
}
